package com.onejson.ojmall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onejson.ojmall.common.domain.PageResult;
import com.onejson.ojmall.common.domain.ResponseDTO;
import com.onejson.ojmall.common.util.OjPageUtil;
import com.onejson.ojmall.dao.UserDao;
import com.onejson.ojmall.model.entity.UserEntity;
import com.onejson.ojmall.model.form.UserBatchForm;
import com.onejson.ojmall.model.form.UserForm;
import com.onejson.ojmall.model.form.UserQueryForm;
import com.onejson.ojmall.model.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 用户表 Service 层
 *
 * @author wxx
 */
@Service
@Slf4j
public class UserService extends ServiceImpl<UserDao, UserEntity> {

    @Resource
    private UserDao userDao;

    private static final String PASSWORD_SALT_FORMAT = "smart_%s_admin_$^&*";


    public static String getEncryptPwd(String password) {
        return DigestUtils.md5Hex(String.format(PASSWORD_SALT_FORMAT, password));
    }

    /**
     * 获取用户表列表
     */
    public PageResult<UserVO> getList(UserQueryForm queryForm) {
        Page<?> page = OjPageUtil.convert2PageQuery(queryForm);
        List<UserVO> list = userDao.query(page, queryForm);
        return OjPageUtil.convert2PageResult(page, list);
    }

    /**
     * 查询用户表
     */
    public UserVO queryById(String detailId) {

        UserEntity detailEntity = userDao.selectById(detailId);
        UserVO userVO = new UserVO();

        try {
            BeanUtils.copyProperties(userVO, detailEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return userVO;

    }


    /**
     * 【用户表】 - 添加
     */
    public ResponseDTO<String> addUser(UserForm addForm) {

        UserEntity entity = new UserEntity();

        addForm.setId(null);

        try {
            BeanUtils.copyProperties(entity, addForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        userDao.insert(entity);
        return ResponseDTO.ok();
    }

    /**
     * 【用户表】 - 更新
     */
    public ResponseDTO<String> updateUser(UserForm updateForm) {

        UserEntity entity = new UserEntity();

        try {
            BeanUtils.copyProperties(entity, updateForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        userDao.updateById(entity);
        return ResponseDTO.ok();
    }


    /**
     * 【用户表】- 批量更新
     */
    public ResponseDTO<String> batchUpdateUser(UserBatchForm userBatchForm) {

        UserEntity entity = new UserEntity();
        List<UserEntity> userEntityList = new ArrayList<>();

        try {
            for (UserForm userForm : userBatchForm.getUserFormList()) {

                userForm.setCreateTime(null);
                userForm.setUpdateTime(null);
                entity.setRemark(userBatchForm.getRemark());
                BeanUtils.copyProperties(entity, userForm);
                userEntityList.add(entity);
            }
        } catch (Exception e) {
            System.out.println("错误消息");
            System.out.println(e.getMessage());
        }
        this.updateBatchById(userEntityList);
        return ResponseDTO.ok();
    }


    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        userDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        userDao.deleteById(id);
        return ResponseDTO.ok();
    }


    public UserEntity getByLoginName(String loginName) {
        return userDao.getByLoginName(loginName);
    }
}

